package org.dreamfly.core.action;

import org.apache.struts2.ServletActionContext;
import org.dreamfly.core.bean.RemoteServiceInfo;
import org.dreamfly.core.bean.ResultSetPool;
import org.dreamfly.core.bean.SzAuthorities;
import org.dreamfly.core.bean.SzUsers;
import org.dreamfly.core.dao.UserDAO;
import org.dreamfly.core.remote.RuntimeServiceRegister;
import org.dreamfly.core.service.PictureService;
import org.dreamfly.core.service.UserService;
import org.dreamfly.core.service.VideoService;
import org.dreamfly.core.util.XMLReader;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 2010-12-3
 * Time: 3:32:31
 * To change this template use File | Settings | File Templates.
 */
public class BootUpRegister extends BaseAction {

    private RuntimeServiceRegister runtimeServiceRegister;
    private ResultSetPool resultSetPool;
    private String server;
    private UserService userService;

    public String initService() {

        System.setProperty("jna.library.path","C:\\Windows\\System32");

        //读取所有的pictureService
        List<RemoteServiceInfo> infoList = XMLReader.readServerInfo(ServletActionContext.getServletContext().getRealPath("") + "/WEB-INF/picture.xml");


        for (RemoteServiceInfo info : infoList) {
            Class serviceClass = PictureService.class;
            String serviceId = info.getServerId();
            String serviceType = info.getServerType();
            String serviceUrl = info.getServerUrl();
            Properties props = null;

            runtimeServiceRegister.registService(serviceClass, serviceId, serviceType, serviceUrl, props);
        }
        //读取所有的videoService
        infoList = XMLReader.readServerInfo(ServletActionContext.getServletContext().getRealPath("") + "/WEB-INF/video.xml");
        for (RemoteServiceInfo info : infoList) {
            Class serviceClass = VideoService.class;
            String serviceId = info.getServerId();
            String serviceType = info.getServerType();
            String serviceUrl = info.getServerUrl();
            Properties props = null;

            runtimeServiceRegister.registService(serviceClass, serviceId, serviceType, serviceUrl, props);
        }
        //读取服务器的IP 和端口信息
        Map<String, Object> map = XMLReader.readServerIpInfo(ServletActionContext.getServletContext().getRealPath("") + "/WEB-INF/ip_name.xml");
        resultSetPool.setIpInfos(map);

        insertAdmin();

        return SUCCESS;
    }

    private void insertAdmin() {
        SzUsers user = new SzUsers();
        Set<SzAuthorities> authoritiesSet = new HashSet<SzAuthorities>();
        addAdminRole(user, authoritiesSet);
        addAdminInfo(user, authoritiesSet);
        if (null == userService.findAdmin()) {
            userService.addUser(user);
        }
    }

    private void addAdminInfo(SzUsers user, Set<SzAuthorities> authoritiesSet) {
        user.setIsNew(false);
        user.setUsername("admin");
        user.setPassword("admin");
        user.setServer(server);
        user.setIsAdmin(true);
        user.setSzAuthoritieses(authoritiesSet);
    }

    private void addAdminRole(SzUsers user, Set<SzAuthorities> authoritiesSet) {
        SzAuthorities authorities = new SzAuthorities();
        authorities.setSzUsers(user);
        authorities.setAuthor("ROLE_ADMIN");
        authoritiesSet.add(authorities);
    }

    public void setRuntimeServiceRegister(RuntimeServiceRegister runtimeServiceRegister) {
        this.runtimeServiceRegister = runtimeServiceRegister;
    }

    public void setResultSetPool(ResultSetPool resultSetPool) {
        this.resultSetPool = resultSetPool;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
